package com.wonyesoft.wuliu.acticity.bargain;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import tools.SD_Finder;

import com.wonyesoft.wuliu.api.GagApi;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnInfoListener;
import android.os.Environment;
import android.provider.ContactsContract.Data;
import android.text.format.Time;
import android.util.Log;
import android.widget.Toast;

/**
 * @author 作者陈修园 E-mail:
 * @date 创建时间：2015-5-30 上午11:33:21
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class OrderSoundRecorder {

	private MediaRecorder mRecorder;
	private Time time;
	public String fileName;
	private Context context;
	private int state = -1;

	public OrderSoundRecorder(Context context) {

		this.context = context;
	}

	public void StartRecorder() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
		String t = format.format(new Date());
		t = t + ".arm";
		Log.v("msg", t);
		String root = SD_Finder.GetPaht();
		fileName = root + GagApi.RecordSoundPaht + t;
		if (root == null) {
			Toast.makeText(context, "没有发现SD卡，无法使用该功能", Toast.LENGTH_LONG).show();
			return;
		}
		if (state == 1) {
			StopRecorder();
		}
	
		if (state == 0 || state == -1) {
			mRecorder = new MediaRecorder();
			state=1;
			mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
			mRecorder.setOutputFile(fileName);
			mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			try {
				mRecorder.prepare();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		mRecorder.start();
	}

	public void StopRecorder() {
		if (mRecorder != null) {
			state = 0;
			//mRecorder.stop();
			mRecorder.reset();
			//mRecorder.
		}

	}

	public void Release() {
		if (mRecorder != null) {
			mRecorder.release();
			mRecorder = null;
		}

	}

	public String GetSonunFileName() {
		if (this.fileName != null) {
			return this.fileName;
		}
		return "";

	}

}
